﻿// SendForm.h : Declaration of the CSendForm
#pragma once

#include "resource.h"       // main symbols

#include <atlhost.h>
#include <vector>
#include <string>

#include "Regesiter.h"
#include "Parser.h"
#include "ServiceProvider.h"
#include "../omsproxy/OMSClientBase.h"
#include "ContactsForm.h"
using namespace ATL;

// CSendForm

class CSendForm : 
	public CAxDialogImpl<CSendForm>
{
public:
	CSendForm(Outlook::_Application* parent)
	{
		regesiter = NULL;
		parser = NULL;
		serviceProviders = NULL;
		m_client = NULL;
		m_app = parent;
		contactForm = NULL;
	}

	CSendForm()
	{
		regesiter = NULL;
		parser = NULL;
		serviceProviders = NULL;
		m_client = NULL;
		m_app = NULL;
		contactForm = NULL;
	}

	~CSendForm()
	{
		if(regesiter)
			delete regesiter;
		if(parser)
			delete parser;
		if(contactForm)
			delete contactForm;
		if(m_client)
			delete m_client;
		if(serviceProviders){
			for(std::vector<ServiceProvider*>::iterator i = serviceProviders->begin();
				i!=serviceProviders->end();
				i++){
				delete (*i);
				(*i) = NULL;
			}
			delete serviceProviders;
		}

	}

	enum { IDD = IDD_SENDFORM };

BEGIN_MSG_MAP(CSendForm)
	MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
	//COMMAND_HANDLER(IDOK, BN_CLICKED, OnClickedOK)
	COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)
	CHAIN_MSG_MAP(CAxDialogImpl<CSendForm>)
	COMMAND_HANDLER(IDC_BUTTON_SEND, BN_CLICKED, OnBnClickedButtonSend)
	COMMAND_HANDLER(IDC_COMBO_PROS, CBN_SELCHANGE, OnCbnSelchangeComboPros)
	COMMAND_HANDLER(IDC_BUTTON_RECEIVERS, BN_CLICKED, OnBnClickedButtonReceivers)
END_MSG_MAP()

// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CAxDialogImpl<CSendForm>::OnInitDialog(uMsg, wParam, lParam, bHandled);
		bHandled = TRUE;
		LoadNativeForm();
		return 1;  // Let the system set the focus
	}

	//LRESULT OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

	LRESULT OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		EndDialog(wID);
		return 0;
	}

private:
	std::vector<ServiceProvider*>* serviceProviders;
	Regesiter* regesiter; 
	Parser* parser;
	OMSClientBase* m_client;
	Outlook::_Application* m_app;
	CContactsForm* contactForm;
	//CFormAddin* m_parent;
	//IDispatch* m_app;
	
private:
	void splitEx(const std::wstring& src,const std::wstring& separate_character, std::vector<std::wstring*>* outputlist);
	void LoadNativeForm();
	void NotifySendFailedInfo(const std::wstring& response);
	ServiceProvider* GetCurServiceProvider();
	LRESULT BuildXmsDataInternal(
		std::wstring& userName , 
		std::wstring& Password , 
		std::wstring& ReplyPhone , 
		std::wstring& Content , 
		std::vector<std::wstring*>* receiverlist ,
		std::wstring& result);

public:
	LRESULT OnBnClickedButtonSend(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnCbnSelchangeComboPros(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnBnClickedButtonReceivers(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
};

